package com.cskaoyan.app.service.impl;

import com.cskaoyan.app.bean.po.MarketSystem;
import com.cskaoyan.app.bean.po.MarketSystemExample;
import com.cskaoyan.app.bean.vo.AdminConfigExpressVO;
import com.cskaoyan.app.bean.vo.AdminConfigMallVO;
import com.cskaoyan.app.bean.vo.AdminConfigOrderVO;
import com.cskaoyan.app.bean.vo.AdminConfigWxVO;
import com.cskaoyan.app.mapper.MarketSystemMapper;
import com.cskaoyan.app.service.AdminConfigService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;

@Service
public class AdminConfigServiceImpl implements AdminConfigService {

    @Autowired
    private MarketSystemMapper systemMapper;

    @SneakyThrows
    @Override
    public AdminConfigMallVO mall() {
        AdminConfigMallVO mallVO = new AdminConfigMallVO();
        String target = "%market_mall%";
        return select(mallVO, target);
    }

    @SneakyThrows
    @Override
    public void mall(AdminConfigMallVO mallVO) {
        update(mallVO);
    }


    @SneakyThrows
    @Override
    public AdminConfigExpressVO express() {
        AdminConfigExpressVO expressVO = new AdminConfigExpressVO();
        String target = "%market_express%";
        return select(expressVO, target);
    }

    @SneakyThrows
    @Override
    public void express(AdminConfigExpressVO expressVO) {
        update(expressVO);
    }

    @Override
    public AdminConfigOrderVO order() {
        AdminConfigOrderVO orderVO = new AdminConfigOrderVO();
        String target = "%market_order%";
        return select(orderVO, target);
    }

    @Override
    public void order(AdminConfigOrderVO orderVO) {
        update(orderVO);
    }

    @Override
    public AdminConfigWxVO wx() {
        AdminConfigWxVO wxVO = new AdminConfigWxVO();
        String target = "%market_wx%";
        return select(wxVO, target);
    }

    @Override
    public void wx(AdminConfigWxVO wxVO) {
        update(wxVO);
    }

    @SneakyThrows
    private <T> T select(T vo, String target) {
        MarketSystemExample example = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = example.createCriteria();
        criteria.andKeyNameLike(target);
        List<MarketSystem> systemList = systemMapper.selectByExample(example);

        Class<?> voClass = vo.getClass();
        for (MarketSystem marketSystem : systemList) {
            Field field = voClass.getDeclaredField(marketSystem.getKeyName());
            field.setAccessible(true);
            field.set(vo, marketSystem.getKeyValue());
        }
        return vo;
    }

    @SneakyThrows
    private <T> void update(T vo) {
        MarketSystem system = new MarketSystem();
        MarketSystemExample example = new MarketSystemExample();

        Class<?> voClass = vo.getClass();
        Field[] fields = voClass.getDeclaredFields();

        for (Field field : fields) {
            field.setAccessible(true);
            String keyName = field.getName();
            Object keyValue = field.get(vo);

            system.setUpdateTime(new Date());
            system.setKeyValue((String) keyValue);

            MarketSystemExample.Criteria criteria = example.createCriteria();
            criteria.andKeyNameEqualTo(keyName);
            systemMapper.updateByExampleSelective(system, example);
            example.clear();
        }
    }
}
